Creating a Flash Element
Andreas Heim
Macromedia is working hard to make you more productive. A big part of this effort is taking the integration between products of the Macromedia MX family further step by step. If you have used Studio MX 2004, or even just Macromedia Flash MX 2004 and Dreamweaver MX 2004, you know already about the interface integration. This article is about a brand new feature called Flash elements, which strengthens the content integration between the two products.
What is a Flash element? It is a Flash component that you can directly insert and customize into a page you are creating with Dreamweaver. While you have been able to insert a Macromedia Flash movie in Dreamweaver for a long time, you can now configure the Flash element directly within Dreamweaver using the Tag inspector. There is no need to go back to Flash to change the configuration of the Flash movie or mess with the source code to pass some extra variables to Flash. This tight integration empowers Dreamweaver users to modify Flash elements, which improves workflow and reduces the time spent passing the elements back and forth between the Dreamweaver and Flash developer for simple edits.
The new Flash elements also make it much easier to create reusable Flash content for your own use as well as for distribution as an extension. In this article, you will learn how to modify a Flash MX 2004 component to become a Flash element, and how to package it as extension to install with Dreamweaver MX 2004.
You have probably have heard about the patent lawsuit between Eolas and Microsoft, and the changes that Microsoft may be forced to make to the Internet Explorer browser. At the time of writing of this article, things are still in flux and Dreamweaver creates HTML output the way things used to be. After integrating a Flash element into your page, I recommend updating the page for deployment the way you should update any other page. You can find out about the techniques of doing so in the Active Content Developer Center.
Requirements
To complete this tutorial you will need to install the following software and files:
Macromedia Flash MX Professional 2004
Macromedia Extension Manager
(included in the above products)
Note: You can also follow this tutorial with Flash MX 2004 Standard version. However, you won’t have the ability to edit the class files with the built in script editor; you will need to use an external text editor.
Tutorials and sample files:
flash_elements_sample.zip (166K
ZIP)
These files contain the FooterNav component created in the article Building the FooterNav Component. You will use the sample component to create a Flash element in this article. If you are interested in learning the entire process of creating a Flash component, modifying that component as Flash element, and packaging it as a Dreamweaver extension, read the article, Building the FooterNav Component prior to completing this article.
Preparing a Flash Component for a Life as a Flash Element
A Macromedia Flash MX 2004 component is almost ready to be a Flash element. There are a few subtle changes you have to make to the meta data in the class file however. Meta data are special "tags" placed inside the source code; programs that use the component can use the information in meta data for special purposes. For instance, [Inspectable] meta data exposes properties as component parameters in Macromedia Flash MX 2004. Dreamweaver uses the same meta data so you can make similar settings for a Flash element. Here is a quick overview of what you need to do:
- Replace double quotes (") in meta data with single quotes (')
- Move [Inspectable] tags to the getter methods rather setter methods
- Change link related types to URI instead of String
- Add [ArrayProps] tags to array properties.
None of these changes will keep your component from functioning in Flash MX 2004, it will continue working there just fine. Yet these changes are necessary for your component to work as Flash element also.
Before you get into updating the FooterNav component, let me explain first why these changes are necessary.
Single Quotes
You don't really have to understand the why this change is necessary in this case—you can just do it. But since you're already reading this section, you're interested in the why, so here you go.
This is a bit tricky to explain, so bear with me. A Flash element is embedded into an HTML page. The solution Macromedia chose to pass the settings you make to the Flash element is to set a single variable that contains all your settings in the embed/object tag as part of the FlashVars attribute/param. Are you still with me? This attribute is defined in the HTML using double quotes. Hence the easiest way to use quotes inside the variable is by using single quotes; you always use the "other type" of quotes inside a string contained with one type of quotes.
An example to illustrate this:
meta data in source code
public function set captionFont( myFont:String ):Void {
__captionFont = myFont;
invalidate();
}
[Inspectable(type='String', defaultValue='Verdana' )]
public function get captionFont():String {
return __captionFont;
}
Result in the <OBJECT> tag:
<param name="FlashVars" value="flashlet={captionFont:'Verdana'}">
and in the <EMBED> tag:
flashvars="flashlet={captionFont:'Verdana'}"
You look at this code if you look at the source code after inserting a Flash element into your page. A Flash element has some special added parsing code that makes a real object out of this variable.
Note:You don't need to replace all double quotes in your code with single quotes. This is for meta data tags only!
Placement of [Inspectable] Tags
The parsing code of the Flash element needs to read two pieces of information
about each property: name and type. It gathers this information with
the get method of a property. The set method
of a property contains additional information: a parameter with a name
and type. Since it's a method, it could theoretically have more than
one parameter. Either way, that additional information is unnecessary
for being able to use the property, and worse, seems to confuse the parsing
code at the moment.
To be safe, just use your meta data with the get method,
Example:
public function set captionText( caption:String ) {
__captionText = caption;
invalidate();
}
[Inspectable(type='String', defaultValue='caption goes here' )]
public function get captionText():String {
return __captionText;
}
Type Setting for Links
A string is just that, a string. A URL has different rules as to what characters are allowed and what needs to be encoded (escaped). By setting the type to URI for links, you allow the Flash element parsing code to take care of it, as simple as that. For example:
public function set captionLink( link:String ) {
__linkText = caption;
}
[Inspectable(type='URI', defaultValue='http://www.macromedia.com' )]
public function get captionText():String {
return __captionText;
}
This example is actually not taken from the footer nav component.
Note: This is a setting you use for the actual links only. Descriptions or a target window are not part of the URL, and thus keep their type as string.
Defining the Type for Array Elements
When you define Array elements in Flash using the component parameters, Flash isn't very picky on their type. The elements are not typed, which means they are actually strings, but you can use them as numbers if the value happens to be a number.
By defining a type for Array elements, you can choose a type and the parser in the Flash element will convert the array elements to that type. For example:
public function set buttonLabels( labels:Array ):Void {
__labels = labels;
invalidate();
}
[Inspectable( type='Array', defaultValue='Macromedia,Google,Yahoo,Nirvana' )]
[ArrayProps(elementType='String')]
public function get buttonLabels():Array {
return __labels;
}
Further Documentation
For more info on component meta data in general, see www.macromedia.com/devnet/mx/flash/articles/buildtest_comp_05.html or check the help in Macromedia Flash MX 2004: Using Components > Creating Components > Component meta data.
You will also find additional information on the structure and elements of a SWC file in the help files in Macromedia Flash MX 2004: Using Components > Creating Components > Exporting the component > Understanding SWC files.
Note: You may have to update your help files in order to see this content in Flash MX 2004.
Updating the FooterNav Component
With the instructions above, you can make all the necessary changes to the meta data in the FooterNav.as class file. For your reference, here is how it should look:
class FooterNav extends mx.core.UIComponent
{
var className:String = "FooterNav";
static var symbolOwner:Object = FooterNav;
static var symbolName:String = "FooterNav";
private var __buttonStyles:Object = {
color : 0x2B333C
,fontSize : 9
,fontFamily : "Verdana"
,fontWeight : "bold"
}
private var __captionStyles:Object = {
color : 0x2B333C
,fontSize : 9
,fontFamily : "Verdana"
,fontWeight : "normal"
}
private var __borderWidth:Number = 2;
private var __buttonAlign:String;
private var __labels:Array;
private var __links:Array;
private var __linkTarget:String;
private var __captionText:String;
private var __captionAlign:String;
private var __margin:Number = 5;
private var __spacing:Number = 2;
private var captionLabel:MovieClip;
private var bg_mc:MovieClip;
private var bgBorder_mc:MovieClip;
private var container_mc:MovieClip;
var clipParameters:Object = { buttonLinks:1, buttonLabels:1, buttonLinkTarget:1, buttonAlignment:1, captionText:1, captionAlignment:1 };
function FooterNav()
{
trace( "new FooterNav" );
}
// :: inspectable properties ::
public function set buttonAlignment( align:String ) {
__buttonAlign = align;
invalidate();
}
[Inspectable( enumeration='left,center,right', defaultValue='left' )]
public function get buttonAlignment():String {
return __buttonAlign;
}
public function set buttonLabels( labels:Array ):Void {
__labels = labels;
invalidate();
}
[Inspectable( type='Array', defaultValue='Macromedia,Google,Yahoo,Nirvana' )]
[ArrayProps(elementType='String')]
public function get buttonLabels():Array {
return __labels;
}
public function set buttonLinks( links:Array ) {
__links = links;
}
[Inspectable(type='Array', defaultValue='http://macromedia.com,http://google.com,http://yahoo.com' )]
[ArrayProps(elementType='URI')]
public function get buttonLinks():Array {
return __links;
}
public function set buttonLinkTarget( tgt:String ) {
__linkTarget = tgt;
}
[Inspectable(enumeration='_blank,_self,_top,_parent', defaultValue='_blank' )]
public function get buttonLinkTarget():String {
return __linkTarget;
}
public function set captionText( caption:String ) {
__captionText = caption;
invalidate();
}
[Inspectable(type='String', defaultValue='caption goes here' )]
public function get captionText():String {
return __captionText;
}
public function set captionAlignment( align:String ) {
__captionAlign = align;
invalidate();
}
[Inspectable( enumeration='left,center,right', defaultValue='left' ) ]
public function get captionAlignment():String {
return __captionAlign;
}
// :: methods ::
function createChildren(Void):Void
{
// create caption label
attachMovie("Label", "captionLabel", 2);
captionLabel.autoSize = "left";
}
function draw(Void):Void
{
// set up the caption label
for (var style in __captionStyles ) {
captionLabel.setStyle( style, __captionStyles[ style ] );
}
captionLabel.text = __captionText;
// create buttons
var container:MovieClip = this.createEmptyMovieClip( "container_mc", 0 );
var item:MovieClip;
var len:Number = __labels.length;
for ( var i:Number=0; i<len; i++ ) {
item = container.attachMovie( "Button", "item"+i, i );
// set button styles
for (var style in __buttonStyles ) {
item.setStyle( style, __buttonStyles[ style ] );
}
item.label = __labels[ i ];
if ( __links[ i ] != undefined ) {
item.data = __links[ i ];
}
else {
item.enabled = false;
}
item.fitContents();
item.addEventListener("click", handleEvent);
}
size();
}
function size(Void):Void
{
trace( "size" );
doLayout();
super.size();
}
function doLayout():Void
{
// place buttons
var container:MovieClip = this.container_mc;
var item:MovieClip;
var len:Number = __labels.length;
var xPos:Number = __margin;
var yPos:Number = __margin;
for ( var i:Number=0; i<len; i++ ) {
item = container[ "item"+i ];
item.move( xPos, yPos );
xPos += item.width + __spacing;
}
// align buttons
if ( __buttonAlign == "left" ) {
container._x = 0;
}
else if ( __buttonAlign == "center" ) {
container._x = Math.floor( ( __width - xPos ) / 2 );
}
else if ( __buttonAlign == "right" ) {
container._x = __width - xPos;
}
// place label
var x:Number;
var y:Number;
y = __height - captionLabel.height - __margin + 4; // offset for extra height of label
if ( __captionAlign == "center" ) {
x = Math.floor ( ( __width - captionLabel.width) /2);
}
else if ( __captionAlign == "right" ) {
x = Math.floor( __width - captionLabel.width - __margin );
}
else {
x = __margin;
}
captionLabel.move ( x, y );
// size and place background and border
bg_mc._x = bg_mc._y = __borderWidth;
bg_mc._width = __width - __borderWidth*2;
bg_mc._height = __height - __borderWidth*2;
bgBorder_mc._x = bgBorder_mc._y = 0;
bgBorder_mc._width = __width;
bgBorder_mc._height = __height;
}
function handleEvent( event_obj ):Void
{
var link:String = event_obj.target.data;
if ( link != null && link != "" ) {
getURL( link, __linkTarget );
}
}
}
Exporting the FooterNav Component as Flash Element
The process of exporting a component is actually no different from exporting the component for placement in the component panel. All you need to do is export it as SWC file. You just don't copy it into the folder for the components panel.
- Open FooterNav.fla.
- Open the library.
- Right-click (on Mac, Control-click) the FooterNav component symbol.
- From the menu, choose Export to SWC File.
- Choose FooterNav.swc as the name and click OK.
Creating the Extension
Now that you have created the Flash element, you need to get it into Dreamweaver. The best way to do this is to create an extension, which will take care of both placing the SWC file in the proper location as well as adding it to the Dreamweaver Insert menu. You then use the Extension Manager to install your extension, and you can choose to share it with others using the Macromedia Exchange.
Macromedia has created some extensive documentation on how to create an extension for any of their products at,http://download.macromedia.com/pub/exchange/mxi_file_format.pdf, and in particular how to extend Dreamweaver at http://www.macromedia.com/support/documentation/en/dreamweaver/. The documentation is a great reference if you want to dive into this deeper. To get started, it is a tad bit too extensive, however.
What you will use is a simple template for a Flash element extension that you can modify for your own Flash elements. Thanks go to Winsha Chen from the Dreamweaver engineering team for helping me to put this together.
You need the following in order to create an extension:
- The Extension Manager (EM)
The EM not only takes care of installing (and removing) your extensions, but also creates them. You should have it installed by now. If not, get it at: www.macromedia.com/exchange/em_download/ - The files that make up your extension.
In our case, that will be just one file, FooterNav.swc. You have that already - An extension installation file.
This is an XML file with an .MXI file suffix containing the instructions on how to install the extension. That's the missing piece; I will get to that in a moment.
Setting up the Extension Installation File
Open the extension installation file. It is in XML format, but of course in a special format for the Extension Manager. The following is the complete file—save it as FooterNav.mxi, it's also included with the files for this article. I will go over each section with you. As already mentioned, you can find additional and more extensive documentation in the documents listed above.
Note:XML files are a bit more finicky with regards to proper syntax than HTML you write for a browser. It is a good idea to start working from a template, rather than writing it from scratch. Also as a quick tip—to test your syntax, rename the file with an XML extension and open it in Internet Explorer or Mozilla. The built-in XML parser in these browsers will either display the XML in a tree form or tell you where it found errors.
<macromedia-extension
name="Footer Nav Flash Element"
version="1.0"
type="FlashElement"
requires-restart="true">
<author name="Andreas Heim" />
<products>
<product name="Dreamweaver" version="7" primary="true" />
</products>
<description>
<![CDATA[
This Extension installs a Flash Element (FooterNav.swc) into the User Config\Objects\FlashElements\ folder.
]]>
</description>
<ui-access>
<![CDATA[
Access the Footer Nav Flash Element by selecting Flash Elements in the Insert Bar, then clicking the Footer Nav button.<br><br>
You can also access it by selecting Insert -> Media -> Footer Nav
]]>
</ui-access>
<files>
<file source="FooterNav.swc" destination="$dreamweaver\Configuration\Objects\FlashElements\" />
</files>
<configuration-changes>
<menu-insert insertAfter="DWMenu_Insert_Media_ImageViewer">
<menuitem id="DWMenu_Insert_FlashElement_FooterNav" name="Footer Nav" key="" command="dw.getDocumentDOM().insertFlashElement('FooterNav.swc')" enabled="(dw.getFocus() != 'browser') && (dw.getDocumentDOM() != null &&
dw.getDocumentDOM().getParseMode() == 'html')"
/>
</menu-insert>
<insertbar-changes>
<insertbar-item-insert insertAfter="FLT_imageViewer">
<button id="FlashElement_FooterNav" name="Footer Nav" file="FlashElements\FooterNav.swc" command="dw.getDocumentDOM().insertFlashElement('FooterNav.swc')" />
</insertbar-item-insert>
</insertbar-changes>
</configuration-changes>
</macromedia-extension>
Now look at this element by element.
<macromedia-extension
name="Footer Nav Flash Element"
version="1.0"
type="FlashElement"
requires-restart="true">
It's pretty straight forward, defining the name, type and version of the extension. The EM uses the type to categorize it and also displays an icon for it. This extension changes some configuration files of Dreamweaver, so it will only become active after you start the application the next time.
<author name="Andreas Heim" />
That will be you!
<products>
<product name="Dreamweaver" version="7" primary="true" />
</products>
You can install an extension for multiple products, so you need a separate tag for it. Here, you define Dreamweaver as the only product, require at least version 7 (MX 2004), and ensure it actually installs there, even if you have a later version installed.
<description>
<![CDATA[
This Extension installs a Flash Element (FooterNav.swc) into the User Config\Objects\FlashElements\ folder.
]]>
</description>
<ui-access>
<![CDATA[
Access the Footer Nav Flash Element by selecting Flash Elements in the Insert Bar, then clicking the Footer Nav button.<br><br>
You can also access it by selecting Insert -> Media -> Footer Nav
]]>
</ui-access>
These to fields contain what is displayed in the bottom description
field in the EM when you select the extension. The first one is simply
a description; the second one is intended to tell the user how to get
to the extension. The <![CDATA[ ]]> tag has a special
function in XML: It tells the parser to ignore anything inside and just
pass it as-is as a text node. This means, for example, you can
use HTML formatting here such as inserting line breaks with <br> and
you also don't have to worry about encoding special characters. In other
words—it makes your life easier—don't take it out.
<files>
<file source="FooterNav.swc" destination="$dreamweaver\Configuration\Objects\FlashElements\" />
</files>
This section tells the EM what files to include with the extension and
where to place them, as simple as that. You can list multiple files.
In this case, you only have one Flash Element to install. The source
path is relative to the path of the MXI file. Please note that $dreamweaver in
the destination path is not where the application is installed, but rather
your personal configuration folder.
<configuration-changes>
<menu-insert insertAfter="DWMenu_Insert_Media_ImageViewer">
<menuitem id="DWMenu_Insert_FlashElement_FooterNav" name="Footer Nav" key="" command="dw.getDocumentDOM().insertFlashElement('FooterNav.swc')" enabled="(dw.getFocus() != 'browser') && (dw.getDocumentDOM() != null &&
dw.getDocumentDOM().getParseMode() == 'html')"
/>
</menu-insert>
<insertbar-changes>
<insertbar-item-insert insertAfter="FLT_imageViewer">
<button id="FlashElement_FooterNav" name="Footer Nav" file="FlashElements\FooterNav.swc" command="dw.getDocumentDOM().insertFlashElement('FooterNav.swc')" />
</insertbar-item-insert>
</insertbar-changes>
</configuration-changes>
This is the tricky part of the installation file. First you defined all the settings for EM, then placed your files; now, you are changing the Dreamweaver configuration to add the Footer Nav Flash Element in two different places.
Although complicated looking at first if you read this, it is quite
self-explanatory. The first tag <menu-insert> adds
the Flash element to the Insert/Media menu, after the Image Viewer Flash
element. Nested, the Footer Nav is listed as actual menu item, the name
attribute defines what you see displayed, the id is a unique
identifier, so it can be addressed. Next, there is a command attribute
that looks a lot like JavaScript. Well, it is actually JavaScript. Dreamweaver
is entirely extensible and controllable through a JavaScript API. Pretty
much anything you do in Dreamweaver has a corresponding JavaScript command.
So you see defined here is the code to execute once the end user clicks
the menu item.
dw.getDocumentDOM().insertFlashElement('FooterNav.swc')
This tells Dreamweaver to take the current document, (getDocumentDOM()) and insert our ('FooterNav.swc') Flash Element (insertFlashElement). If you look at it that way, it seems quite logical. You can learn more details about this in the Extending Dreamweaver documentation.
Finally, there is a very cryptic looking enabled attribute. It defines when the menu item will be active and when grayed out. It makes sure that you can insert the Flash element only in an open document that can be parsed as HTML. For example if you donut have any document open, there is no place to insert the Flash element, so the menu item is unavailable. There is also an empty key attribute; you can use it to define a keyboard shortcut.
Now the second part, <insertbar-changes>, looks very
familiar. The differences are merely because the Insert bar is not the
same as the Insert menu. It doesn't need an enable attribute, since the
insert bar is automatically enabled or disabled based on the context.
At the core, it still means defining where to insert the Flash element
into the bar, what to display, and what code to execute when a user clicks
it.
Compiling the Extension
All the ingredients for the Extension are ready—it is time to put them together. At this point, you should have FooterNav.swc and FooterNav.mxi in the same folder.
- Open the Extension Manager (EM).
- From the File menu, choose Package Extension
- Navigate to the folder where you placed FooterNav.mxi, select the file and click ok.
- You will be asked to save the extension. FooterNav.mxp will already be set as name. Click Save.
A confirmation dialog box informs you that the Extension was created successfully. When you create your own extension installation files, you may every now and then get some error messages and you will have to correct your file.
Note: You can also double-click FooterNav.mxi to launch the EM and start the packaging process.
Installing the Extension
You created the extension, now you will want to install it and try it out.
- Close Dreamweaver if you have it open.
- Go back to the Extension Manager (EM)
- From the File menu, select Install Extension.
- Navigate to FooterNav.mxp, select it, and click Install.
- You will be greeted with a Extension Disclaimer dialog box. I recommend reading it before clicking Accept.
After clicking Accept, the Extension installs and confirms that the installation was successful.
Figure 1. The Extension Manager successful install dialog box
Note: You can also double-click FooterNav.mxp to install it.
Figure 2. The Extension Manager showing the Footer Nav Flash Element
Using the Footer Nav Extension
After creating and installing the Extension, you will want to see it in action, the important part.
- Open Dreamweaver. If you didn't close it before installing the extension, you will have to restart it.
- Create a new HTML document, save it, and switch to the Design view. It is important that you save it.
- Open the Insert menu, find the Media nested menu. Underneath Image Viewer, see the Footer Nav. Click it to insert it. You can also find Footer Nav in the Flash Elements Insert Bar.
- Specify a file name for the Flash Element.
- The Footer Nav is now in your document, displayed as gray box with a Flash logo.
- The Tag inspector will open automatically. Specify the parameters for the Flash element just as you could specify the component parameters in Flash MX 2004. Try it out!
- With the Footer Nav still selected in the HTML, open the Properties inspector. Click Play to preview it in HTML.
- Note: In the Properties inspector, you can also change the size of the Footer Nav.
- Open the HTML document in your browser of choice. The Footer Nav will look just like in the preview.
Note: Because of the patent lawsuit between Eolas and Microsoft, Microsoft may be forced to make some significant changes to the Internet Explorer browser. At the time of writing of this article things are still in flux and Dreamweaver creates an HTML output the way things used to be. After integrating a Flash Element into your page, I recommend updating the page for deployment the way you should update any other page. You can find out about the techniques of doing so in the Active Content Developer Center.
Figure 3. Inserting the Footer Nav Flash Element in a page using Dreamweaver MX 2004
That's it—you took an existing Flash component and turned it into a Flash element that you can easily insert into any website. Now you too can be a Dreamweaver Extension developer.